package com.hmall.mq;

import com.hmall.constants.MqConstants;
import com.hmall.service.ISearchService;
import org.elasticsearch.cluster.NotMasterException;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SpringRabbitListener {

    @Autowired
    private ISearchService iSearchService;

    /**
     * 监听新增或修改的业务
     * @param id id
     */
    // @RabbitListener(queues = MqConstants.ITEM_INSERT_QUEUE)
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = MqConstants.ITEM_INSERT_QUEUE),
                    exchange = @Exchange(name = MqConstants.ITEM_EXCHANGE,type = "topic"),
                    key = MqConstants.ITEM_INSERT_KEY
            )
    })
    public void monitorHotelBusiness(Long id) {
        System.out.println("消费了"+ MqConstants.ITEM_INSERT_QUEUE  +"队列中的消息，新增id=" + id + "文档进入es!!");
        iSearchService.insertById(id);
    }

    /**
     * 监听删除的业务
     * @param id id
     */
    // @RabbitListener(queues = MqConstants.ITEM_DELETE_QUEUE)
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = MqConstants.ITEM_DELETE_QUEUE),
            exchange = @Exchange(name = MqConstants.ITEM_EXCHANGE,type = "topic"),
            key = MqConstants.ITEM_DELETE_KEY
    )})
    public void listenerHotelDelete(Long id) {
        System.out.println("消费了"+ MqConstants.ITEM_DELETE_QUEUE  +"队列中的消息，删除id=" + id + "文档数据!!");
        iSearchService.deleteById(id);
    }
}
